package cn.itcast.controller;

import cn.itcast.constant.StatusConstant;
import cn.itcast.result.PageBean;
import cn.itcast.result.Result;
import cn.itcast.service.SetmealService;
import cn.itheima.goods.dto.SetmealDTO;
import cn.itheima.goods.dto.SetmealPageQueryDTO;
import cn.itheima.goods.pojo.Setmeal;
import cn.itheima.goods.vo.SetmealDishVO;
import cn.itheima.goods.vo.SetmealOverViewVO;
import cn.itheima.goods.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
@AllArgsConstructor
public class SetmealController {
    private SetmealService setmealService;

    @GetMapping("/getCountByStatus")
    public SetmealOverViewVO getCountByStatus(){
        return setmealService.getCountByStatus();
    }

    /**
     * 根据分类id查询套餐
     *
     * @param categoryId 分类id
     * @return 套餐列表
     */
    @GetMapping("/list")
    @ApiOperation("查询分类下状态为启售的套餐列表")
    public Result<List<Setmeal>> list(Long categoryId) {
        Setmeal setmeal = new Setmeal();

        setmeal.setCategoryId(categoryId);
        setmeal.setStatus(StatusConstant.ENABLE);//查询起售中的套餐

        List<Setmeal> list = setmealService.getListByCategoryId(setmeal);

        return Result.success(list);
    }

    /**
     * 根据套餐id查询套餐菜品详情
     * @param id 套餐id
     * @return 菜品列表
     */
    @GetMapping("/dish/{id}")
    @ApiOperation("根据套餐id查询包含的菜品")
    public Result<List<SetmealDishVO>> getDishesById(@PathVariable Long id) {

        List<SetmealDishVO> list = setmealService.getDishesById(id);

        return Result.success(list);
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO 套餐信息
     * @return 无
     */
    @PostMapping
    @ApiOperation("新增套餐")
    public Result<String> add(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐：{}", setmealDTO);
        setmealService.addWithDish(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO 套餐分页条件
     * @return 套餐分页列表
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageBean> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("菜品分页查询:{}", setmealPageQueryDTO);
        PageBean pageBean = setmealService.pageQuery(setmealPageQueryDTO);//后绪步骤定义
        return Result.success(pageBean);
    }

    /**
     * 套餐批量删除
     *
     * @param ids 套餐id集合
     * @return 无
     */
    @DeleteMapping
    @ApiOperation("套餐批量删除")
    public Result<String> delete(@RequestParam List<Long> ids) {
        log.info("套餐批量删除：{}", ids);
        setmealService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     *
     * @param id 套餐id
     * @return 无
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("根据id查询套餐：{}", id);
        SetmealVO setmealVO = setmealService.getByIdWithDish(id);//后绪步骤实现
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO 套餐信息
     * @return 无
     */
    @PutMapping
    @ApiOperation("修改套餐")
    public Result<String> update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐：{}", setmealDTO);
        setmealService.updateWithDish(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐起售、停售
     *
     * @param status 状态
     * @param id     套餐id
     * @return 无
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售、停售")
    public Result<String> updateStatus(@PathVariable Integer status, Long id) {
        setmealService.updateStatus(status, id);
        return Result.success();
    }
}